<?php
/*
 module:		广告
 create_time:	2024-03-12 17:57:31
 author:		
 contact:		
*/

namespace app\api\controller;

use app\api\model\Ad as AdModel;
use think\facade\Db;

class Ad extends Common
{


    /*start*/
    /**
     * @api {get} /Ad/index 01、首页数据列表
     */
    function index()
    {
        $where = [];
        $field = 'id,content as img,url';
        $orderby = 'show_order desc';
        $time = time();
        $where["url"] = ['like', "news"];
        //            alias('a')->join('catagory b','a.class_id=b.class_id')->
        $res = AdModel::where("(deadline > '$time' OR deadline = 0 )")
            ->where("(url like '%news%' OR url like '%jobfair%'  OR url like '%subject%')")
            ->field($field)->order($orderby)->select()->toArray();
        foreach ($res as &$item) {
            $item["img"] = "https://www.lianhezhaopin.com/data/upload/attach_img/" . $item["img"];
        }
        return $this->ajaxReturn($this->successCode, '返回成功', htmlOutList($res));
    }

    /**
     * @api {get} /Ad/view 02、查看详情
     */
    function view()
    {
        $id = $this->request->get('id', 0, 'intval');

        $ad = AdModel::where("id", $id)->field("id,content as img,url")->findOrEmpty()->toArray();
        if (empty($ad) || empty($ad["url"])) {
            $ad["content"] = "<p>数据不存在</p>";
        } else {
            $im = explode("/", $ad["url"]);
            $type=$im[count($im) - 2];
            $im = $im[count($im) - 1];
            $im = explode(".", $im);
            $article_id = $im[0];
            if($type=="jobfair"){
                $content =   Db::name("jobfair")->where("id", $article_id)->value("introduction");
            }else{
                $content = \app\api\model\Article::where("id", $article_id)->value("content");
            }
            $content=htmlspecialchars_decode($content);
            $img_host="https://www.lianhezhaopin.com";
            $content = str_replace('src="/data/upload', 'src="'.$img_host."/data/upload", $content);
            $ad["content"] =$content;
        }
        $ad["img"] = "https://www.lianhezhaopin.com/data/upload/attach_img/" . $ad["img"];
        return $this->ajaxReturn($this->successCode, '返回成功', $ad);
    }
    /*end*/


}

